package ma.ensao.evalmetrics.controller.metrics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import ma.ensao.evalmetrics.model.*;
import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaPackage;
import com.thoughtworks.qdox.model.JavaSource;
import com.thoughtworks.qdox.parser.Builder;
/*
 * 
 * M�trique pour mesurer l�instabilit�
 * Calcule I : mesure la d�pendance d'un package par rapport aux autres packages
 * 
 */

public class IManager {
	D d = new D();
	I i = new I();

	double r;
	public int Cp;
	public int Ce;

	public D getD() {
		return d;
	}

	public void setD(D d) {
		this.d = d;
	}

	public I getI() {
		return i;
	}

	public void setI(I i) {
		this.i = i;
	}

	public int getCp() {
		return Cp;
	}

	public void setCp(int cp) {
		Cp = cp;
	}

	public int getCe() {
		return Ce;
	}

	public void setCe(int ce) {
		Ce = ce;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		r = r;
	}

	double Res;
	File foldrs[];
	ArrayList<String> Packs;

	public ArrayList<String> getPacks() {
		return Packs;
	}

	public void setPacks(ArrayList<String> Packs) {
		Packs = Packs;
	}

	public File[] getFoldrs() {
		return foldrs;
	}

	public void setFoldrs(File[] foldrs) {
		this.foldrs = foldrs;
	}

	String Pack;

	public String getPack() {
		return Pack;
	}

	public void setPack(String pack) {
		Pack = pack;
	}

	public double getRes() {
		return Res;
	}

	public void setRes(double res) {
		Res = res;
	}

	public IManager() {
		// Packs(file, Packs);
	}

	public I read(File file) throws IOException {
		Packs = new ArrayList<>();
		ArrayList<String> ab = new ArrayList<>();
		File file1 = file;
		while (!file1.getName().equals("src")) {
			file1 = file1.getParentFile();
		}
		String Path = file1.getPath();
		Insert(file1, Packs);
		ab = Packs;

		// System.out.println("dddddddd"+ab.size());
		for (int i = 0; i < Packs.size(); i++) {
			Packs.set(
					i,
					Packs.get(i).replace(Path.concat("\\"), "")
							.replace("\\", "."));
		}

		int cp = 0, ce = 0;
		ArrayList<String> list = new ArrayList<>();

		InputStream ips = new FileInputStream(file);
		InputStreamReader ipsr = new InputStreamReader(ips);
		BufferedReader br = new BufferedReader(ipsr);
		String ligne;
		// Packs(file, Packs);
		while ((ligne = br.readLine()) != null) {
			for (String a : Packs) {
				
				if (ligne.contains("import") && ligne.contains(a.concat("."))) {
					if (list.size() == 0 || !list.contains(a)) {
						list.add(a);
						cp++;
					} 
				}
			}

		}
		File file2 = file;
		while (!file2.getName().equals("src")) {
			file2 = file2.getParentFile();
		}
		String Path2 = file2.getPath();
		String packc = file.getParentFile().getPath().replace(Path2 + "\\", "")
				.replace("\\", ".");
		// System.out.println(packc);
		for (int i = 0; i < Packs.size(); i++) {
			ab.set(i, Path.concat("\\").concat(ab.get(i)).replace(".", "\\"));

		}
		for (String a : ab) {
			if (verif(new File(a), packc)) {
				ce++;
			}
		}
		Cp = cp;
		Ce = ce;
		double x,y;
		x=(double)Cp;
		y=(double)Ce;
		
		
		i.setCe(Ce);
		i.setCp(Cp);
		if((x+y)!=0){
			i.setIn(y/(x+y));
		}
		//double d=(double)(y/(y+x));
		//System.out.println("ee"+(double)(y/(y+x)));
		//i.setIn((double)(y/(y+x)));
		//i.setIn(5.0);
		return i;
	}

	public boolean verif(File file, String path) throws IOException {
		boolean test = false;
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				if (f.isFile() && f.getName().endsWith(".java")) {
					InputStream ips = new FileInputStream(f);
					InputStreamReader ipsr = new InputStreamReader(ips);
					BufferedReader br = new BufferedReader(ipsr);
					String ligne;
					while ((ligne = br.readLine()) != null) {
						if (ligne.contains("import")
								&& ligne.contains(path.concat("."))) {
							test = true;
						}

					}
				}
			}
		}
		return test;
	}

	public void Insert(File file, ArrayList<String> Packs) {

		if (file.isFile() && file.getName().endsWith(".java")) {
			if (Packs.size() == 0) {
				Packs.add(file.getParentFile().getPath());
			} else if (!Packs.contains(file.getParentFile().getPath())) {
				Packs.add(file.getParentFile().getPath());
			}
		}
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				Insert(f, Packs);
			}
		}
		// System.out.println(Packs.size());

	}

	public static void main(String[] args) throws IOException {
		File file = new File("C:/Users/sam/Desktop/RatRoidClient/src/com/hicham/ratroid/A.java");
		// System.out.println(file.getPath());
		IManager im = new IManager();
		I ii = im.read(file); 
		
		System.out.println("saad bouna"+ii.getIn()+ii.getCe()+ii.getCp());
	}
}
